<?php 
/**
* 产品详细页控制器
*/
class ContentController extends CommonController{
	
	public function index(){
		// 获得商品id
		$gid = Q('get.gid',0,'intval');
	 	//get获取cid
		$cid = Q('get.cid');
$this->topCate();
		// 获得商品的详细信息
		$goodData = M()->join('__goods__ g JOIN __goods_detail__ gd ON g.gid=gd.goods_gid JOIN __brands__ b ON g.brands_bid=b.bid')->where("gid={$gid}")->find();
		$goodData['small_pic'] = explode(',', $goodData['small_pic']);
		$goodData['mid_pic'] = explode(',', $goodData['mid_pic']);
		$goodData['big_pic'] = explode(',', $goodData['big_pic']);
		// p($goodData);


		// 获得该商品的规格
		$specData = M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.type_attr_taid=ta.taid')->where("goods_gid={$gid}")->all();
		// p($specData);
				// 循环数组，把颜色和大小压入商品变量中
		foreach ($specData as $key => $value) {
			if ($value['taname']=='净含量') {
				$goodData['capacity'][$key]['gaid'] = $value['gaid'];
				$goodData['capacity'][$key]['attr'] = $value['attr'];
			}
			if ($value['taname']=='包装') {
				$goodData['pack'][$key]['gaid'] = $value['gaid'];
				$goodData['pack'][$key]['attr'] = $value['attr'];
			}
		}
		$this->assign('specData',$specData);
		$this->assign('goodData',$goodData);
//		p($goodData);
		// 获得顶级分类
        $this->nav();
		$this->display();
	}

	// 尺码异步
	public function ajax_size(){
		// p(Q('post.'));die;
		// 获得当前商品gid,可以找到当前商品的货品列表
		$gid = Q('post.gid',0,'intval');
		// 组合id,把逗号去掉
		$group =ltrim(Q('post.group'),',');
		// 找到当前商品所对应的货品列表
		$listData = K('GoodsList')->where("goods_gid={$gid}")->all();
		// 定义一个变量
		$many =0;
		// 循环货品列表，如果组合id相同就得到他的数量
		foreach ($listData as $k => $v) {
			if ($v['combine']==$group) {
				$many = $v['inventory'];
			}
		}
		// 获得规格所对应的添加金额
		$arr = explode(',', $group);
		$addmoney = 0;
		foreach ($arr as $k => $v) {
			$find = K('GoodsAttr')->where("gaid={$v}")->find();
			$addmoney = $addmoney+$find['extra_price'];
		}
		$data = array(
			0=>$many,
			1=>$addmoney
			);
		// 反出库存数量
		$this->ajax($data);
	}











}
 ?>